<?php

namespace App\Models\Applet\Sponsor;

class UserSlaughterRecordsModel extends BaseSponsorModel
{
    protected $table = 'user_slaughter_records';

    const TYPE_SEND_BACK = 10; // 宰杀寄回
    const TYPE_RECYCLE   = 20; // 回收

    const STATUS_YES = 10; // 同意
    const STATUS_NO  = 20; // 拒绝

    /**
     * 总条数
     * @param array $params
     * @return int
     */
    public function total(array $params): int
    {
        $query = $this->join('user_sponsor', 'user_slaughter_records.user_sponsor_id', '=', 'user_sponsor.id')
                      ->join('users', 'user_sponsor.user_id', '=', 'users.id')
                      ->leftJoin('slaughter_send_back_records', 'user_sponsor.id', '=', 'slaughter_send_back_records.user_slaughter_records_id')
                      ->leftJoin('slaughter_express', 'slaughter_send_back_records.slaughter_express_id', '=', 'slaughter_express.id');

        if (!empty($params['keyword'])) {
            $query = $query->whereRaw('(nickname like "%' . $params['keyword'] . '%"' .
                                      ' or users.phone like "%' . $params['keyword'] . '%"' .
                                      ' or name like "%' . $params['keyword'] . '%"' .
                                      ' or express_no like "%' . $params['keyword'] . '%")');
        }

        if (isset($params['status'])) {
            $query = $query->where('user_slaughter_records.status', $params['status']);
        }

        return $query
            ->where('user_slaughter_records.deleted', self::DELETED_NO)
            ->count();
    }

    /**
     * 宰杀申请列表
     * @param array $params
     * @param array $fields
     * @return array
     */
    public function list(array $params, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $query = $this->select($fields)
                      ->join('user_sponsor', 'user_slaughter_records.user_sponsor_id', '=', 'user_sponsor.id')
                      ->join('users', 'user_sponsor.user_id', '=', 'users.id')
                      ->leftJoin('slaughter_send_back_records', 'user_sponsor.id', '=', 'slaughter_send_back_records.user_slaughter_records_id')
                      ->leftJoin('slaughter_express', 'slaughter_send_back_records.slaughter_express_id', '=', 'slaughter_express.id');

        if (!empty($params['keyword'])) {
            $query = $query->whereRaw('(nickname like "%' . $params['keyword'] . '%"' .
                                      ' or users.phone like "%' . $params['keyword'] . '%"' .
                                      ' or name like "%' . $params['keyword'] . '%"' .
                                      ' or express_no like "%' . $params['keyword'] . '%")');
        }

        if (isset($params['status'])) {
            $query = $query->where('user_slaughter_records.status', $params['status']);
        }

        return $query
            ->where('user_slaughter_records.deleted', self::DELETED_NO)
            ->orderBy('id', 'desc')
            ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
            ->limit(PAGE_SIZE)
            ->get()
            ->toArray();
    }

    /**
     * 根据用户id和用户认养id查询数据
     * @param int $userId
     * @param int $userSponsorId
     * @param array $fields
     * @return array
     */
    public function getItemByUserIdAndUserSponsorId(int $userId, int $userSponsorId, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
                       ->where('user_id', $userId)
                       ->where('user_sponsor_id', $userSponsorId)
                       ->where('deleted', self::DELETED_NO)
                       ->orderBy('id', 'desc')
                       ->first();
        return $result ? $result->toArray() : [];
    }
}
